﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using HsTec.DigitalPlatform.Model.DTO;
using HsTec.DigitalPlatform.Business;
using HsTec.DigitalPlatform.DataAccess;
using System.Transactions;

namespace HsTec.DigitalPlatform.WebSite.Controllers
{
    public class RegController : Controller
    {
        GradeInfoManager gradeinfomanager = new GradeInfoManager();
        SpecInfoManager specinfomanager = new SpecInfoManager();
        ClassInfoManager classinfomanager = new ClassInfoManager();
        ItemInfoManager iteminfomanager = new ItemInfoManager();
        PlayerInfoManager playerinfomanager = new PlayerInfoManager();
        PlayerItemsInfoManager playeritemsinfomanager = new PlayerItemsInfoManager();
        //
        // GET: /Reg/

        public ActionResult Index()
        {
            RegSession rs = new RegSession();
            rs.op = "new";
            Session["reginfo"] = rs;
            return View();
        }
        public ActionResult RegPlayer()
        {
            RegPlayerModel player = new RegPlayerModel();
            player.gradelist = gradeinfomanager.FindALLGradeInfo().Select(m => new SelectListItem() { Text = m.gradename, Value = m.gradename });
            player.speclist = specinfomanager.FindALLSpecInfo().ToList().Select(m => new SelectListItem() { Text = m.spename, Value = m.speid.ToString() });
            List<SelectListItem> classlist = new List<SelectListItem>();
            classlist.Add(new SelectListItem { Text = "请选择...", Value = "0" });
            player.classlist = classlist;
            return View(player);
        }
        public ActionResult RegItem()
        {
            ViewData["playername"] = ((RegSession)Session["reginfo"]).playername;
            ViewData["sex"] = ((RegSession)Session["reginfo"]).sex;
            ViewData["items"] = ((RegSession)Session["reginfo"]).itemlist;
            return View(ViewData);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetClassInfo(string specid, string gradeid)
        {
            if (!String.IsNullOrEmpty(specid) && !String.IsNullOrEmpty(gradeid))
            {
                return Json(classinfomanager.FindClassInfoBySpecID(int.Parse(specid), int.Parse(gradeid)));
            }
            return null;
        }
        [HttpPost]
        public ActionResult RegPlayer(FormCollection form)
        {
            RegSession rs = (RegSession)Session["reginfo"];
            rs.playerno = form["playerno"];
            rs.playername = form["playername"];
            rs.playergrade = form["playergrade"];
            rs.playerspec = int.Parse(form["playerspec"]);
            rs.playerclass = int.Parse(form["playerclass"]);
            rs.password = form["password"];
            rs.info = form["sltinfo"];
            rs.sex = form["sex"];
            rs.tel = form["tel"];
            Session["reginfo"] = rs;
            return RedirectToAction("RegItem", "Reg");
        }

        [HttpPost]
        public ActionResult RegItem(FormCollection form)
        {
            RegSession rs = (RegSession)Session["reginfo"];
            Dictionary<int, string> list = new Dictionary<int, string>();
            foreach (string str in form["hiditems"].Split(','))
            {
                if (!String.IsNullOrEmpty(str))
                {
                    string[] ary = str.Split('~');
                    list.Add(int.Parse(ary[0]), ary[1]);
                }
            }
            rs.itemlist = list;
            return RedirectToAction("Confirmation", "Reg");
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetItemInfo(string itemtype, string sex)
        {
            if (!String.IsNullOrEmpty(itemtype))
            {
                return Json(iteminfomanager.FindItemInfoByType(itemtype, sex));
            }
            return null;
        }

        public ActionResult Confirmation()
        {
            ViewData["playername"] = ((RegSession)Session["reginfo"]).playername;
            ViewData["playerno"] = ((RegSession)Session["reginfo"]).playerno;
            ViewData["sex"] = ((RegSession)Session["reginfo"]).sex;
            ViewData["tel"] = ((RegSession)Session["reginfo"]).tel;
            string[] ary = ((RegSession)Session["reginfo"]).info.Split(',');
            ViewData["gradename"] = ary[0];
            ViewData["specname"] = ary[1];
            ViewData["classname"] = ary[2];
            ViewData["items"] = ((RegSession)Session["reginfo"]).itemlist.Values.ToList<string>();
            return View(ViewData);
        }

        [HttpPost]
        public ActionResult Confirmation(FormCollection form)
        {
            PlayerInfo playerinfo = new PlayerInfo();
            RegSession rs = (RegSession)Session["reginfo"];
            playerinfo.playeno = rs.playerno;
            playerinfo.playername = rs.playername;
            playerinfo.grade = rs.playergrade;
            playerinfo.specid = rs.playerspec;
            playerinfo.classid = rs.playerclass;
            playerinfo.password = rs.password;
            playerinfo.sex = rs.sex;
            playerinfo.tel = rs.tel;
            using (TransactionScope scope = new TransactionScope())
            {
                int maxid = 0;
                if (rs.op == "new")
                {
                    playerinfomanager.Add(playerinfo);
                    maxid = playerinfomanager.GetMaxID();
                }
                else
                {
                    playerinfo.playerid = rs.playerid;
                    playerinfomanager.Save();
                    playeritemsinfomanager.DeleteByPlayer(rs.playerid);
                }
                List<int> list = rs.itemlist.Keys.ToList<int>();
                foreach (int id in list)
                {
                    PlayerItemsInfo rii = new PlayerItemsInfo();
                    rii.itemid = id;
                    if (rs.op == "new")
                        rii.playerid = maxid;
                    else
                        rii.playerid = rs.playerid;
                    playeritemsinfomanager.Add(rii);
                }
                scope.Complete();
            }
            return RedirectToAction("Complete", "Reg");
        }

        public ActionResult Complete()
        {
            return View();
        }

        [HttpPost]
        public JsonResult CheckInfo(string playerno)
        {
            return Json(playerinfomanager.Exist(playerno));
        }

        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection form)
        {
            string playerno = form["playerno"];
            string password = form["password"];
            ViPlayerInfo playerinfo = playerinfomanager.GetPlayerInfoByPassword(playerno, password);
            List<ViPlayerItmesInfo> list = playeritemsinfomanager.GetItemsInfos(playerinfo.playerid);
            RegSession rs = new RegSession();
            rs.op = "edit";
            rs.playerid = playerinfo.playerid;
            rs.playerno = playerinfo.playeno;
            rs.password = playerinfo.password;
            rs.sex = playerinfo.sex;
            rs.tel = playerinfo.tel;
            rs.playerclass = (int)playerinfo.classid;
            rs.playergrade = playerinfo.grade;
            rs.playername = playerinfo.playername;
            rs.playerspec = (int)playerinfo.specid;
            rs.info = playerinfo.grade + "," + playerinfo.spename + "," + playerinfo.classname;
            Dictionary<int, string> dic = new Dictionary<int, string>();
            foreach (ViPlayerItmesInfo key in list)
            {
                dic.Add((int)key.itemid, key.itemname.Trim());
            }
            rs.itemlist = dic;
            Session["reginfo"] = rs;
            return RedirectToAction("RegItem", "Reg"); ;
        }

        public JsonResult CheckPlayerInfo(string playerno, string password)
        {
            return Json(playerinfomanager.Exist(playerno, password));
        }
    }
}
